package work.base.designpattern.template;

// 抽象类：饮料
abstract class Beverage {
    // 模板方法，定义冲泡饮料的算法框架
    public final void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        if (customerWantsCondiments()) {
            addCondiments();
        }
    }

    // 具体方法：烧水
    public void boilWater() {
        System.out.println("Boiling water");
    }

    // 抽象方法：冲泡
    public abstract void brew();

    // 具体方法：倒入杯子
    public void pourInCup() {
        System.out.println("Pouring into cup");
    }

    // 抽象方法：添加调料
    public abstract void addCondiments();

    // 钩子方法：是否添加调料，默认添加
    public boolean customerWantsCondiments() {
        return true;
    }
}

// 具体类：咖啡
class Coffee extends Beverage {
    @Override
    public void brew() {
        System.out.println("Dripping Coffee through filter");
    }

    @Override
    public void addCondiments() {
        System.out.println("Adding Sugar and Milk");
    }

    // 重写钩子方法，自定义是否添加调料
    @Override
    public boolean customerWantsCondiments() {
        return false;
    }
}

// 具体类：茶
class Tea extends Beverage {
    @Override
    public void brew() {
        System.out.println("Steeping the tea");
    }

    @Override
    public void addCondiments() {
        System.out.println("Adding Lemon");
    }
}

// 测试类
public class TemplateDemo {
    public static void main(String[] args) {
        System.out.println("Making coffee...");
        Beverage coffee = new Coffee();
        coffee.prepareRecipe();

        System.out.println("\nMaking tea...");
        Beverage tea = new Tea();
        tea.prepareRecipe();
    }
}
